package 第一题;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Socket socket = null;
        PrintWriter pw = null;
        ObjectInputStream ois = null;
        OutputStream os = null;
        try {
            socket = new Socket("localhost",8888);
            pw = new PrintWriter(socket.getOutputStream(),true);
            ois=new ObjectInputStream(socket.getInputStream());
            os = socket.getOutputStream();
            while (true){
                System.out.println("---输出全部学生信息精选<1>---");
                System.out.println("---查询学生信息精选<2>---");
                System.out.println("请输入(按0退出):");
                int num = scanner.nextInt();
                if (num==0){
                    pw.println("0");
                    System.out.println("欢迎退出");
                    break;
                }else if (num==1){
                   pw.println("1");

               }else if(num==2){
                    pw.println("2");
                    //os.flush();
                    System.out.println("请输入学生姓名");
                    String name = scanner.next();
                    pw.println(name);
                }
                //接收服务端返回的学生数据集合
                List<Student> studentList=(ArrayList<Student>)ois.readObject();
                System.out.println("姓名\t年龄\t性别\t所属班级");
                for (Student s: studentList) {
                    System.out.println(s.getName()+"\t"+s.getAge()+"\t\t"+s.getGender()+"\t\t"+s.getClassName());
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (socket!=null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
